#include <Unit.h>
#include <ostream>

Unit::Unit(const UnitType* pType, const char* const unitName, double ratio, const Unit* p_nextMaxUnit):
          pType_m(pType),unitName_m(unitName),ratioToBase_m(ratio),p_nextMaxUnit_m(p_nextMaxUnit)
{
    if( p_nextMaxUnit != NULL )
    {
        ratioToBase_m = ratio * p_nextMaxUnit_m->ratioToBase_m;
    }
}

bool Unit::isSameType(const Unit& unit) const
{
    return pType_m == unit.pType_m;
}

double Unit::toBaseVlaue(double vlaue) const
{
    return vlaue * ratioToBase_m;
}

double Unit::getRatioToBase() const
{
    return ratioToBase_m;
}

bool Unit::getBaseUnit(const Unit** p_baseUnit) const
{
    if(p_baseUnit != NULL)
    {
        do{}while(getNextMaxUnit(p_baseUnit));
    }
}

bool Unit::getNextMaxUnit(const Unit** p_unit) const
{
    if(p_unit != NULL && *p_unit != NULL && (*p_unit)->p_nextMaxUnit_m != NULL)
    {
      *p_unit = (*p_unit)->p_nextMaxUnit_m;
      return true;
    }
    else
    { 
        return true;
    }
}
void Unit::toString(std::ostream & os,int value) const
{
    if( value != 0 )
        os << value << " " << unitName_m;
}
